package com.luo.chengrui.module.sa.service.role;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import com.luo.chengrui.module.sa.controller.admin.commontree.vo.CommonTreeBizExportReqVO;
import com.luo.chengrui.module.sa.controller.admin.org.vo.OrgBaseVO;
import com.luo.chengrui.module.sa.controller.admin.person.vo.PersonInfoVO;
import com.luo.chengrui.module.sa.dal.dataobject.commontree.CommonTreeBizDO;
import com.luo.chengrui.module.sa.dal.dataobject.org.OrgDO;
import com.luo.chengrui.module.sa.dal.dataobject.role.RoleFolderUserViewDO;
import com.luo.chengrui.module.sa.service.commontree.CommonTreeBizService;
import com.luo.chengrui.module.sa.service.org.OrgService;
import com.luo.chengrui.module.sa.service.person.PersonService;
import com.luo.chengrui.module.sa.service.role.RoleFolderUserService;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import cn.iocoder.yudao.module.system.enums.permission.RoleCodeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.luo.chengrui.module.sa.controller.admin.role.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.role.RoleFolderUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import com.luo.chengrui.module.sa.convert.role.RoleFolderUserConvert;
import com.luo.chengrui.module.sa.dal.mysql.role.RoleFolderUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.luo.chengrui.module.sa.enums.ErrorCodeConstants.*;;

/**
 * 角色分类人员配置表 Service 实现类
 *
 * @author 诚睿
 */
@Service
@Validated
public class RoleFolderUserServiceImpl implements RoleFolderUserService {

    @Resource
    private RoleFolderUserMapper roleFolderUserMapper;
    @Resource
    private OrgService orgService;
    @Resource
    private CommonTreeBizService commonTreeBizService;

    @Resource
    private PermissionApi permissionApi;


    private RoleFolderUserServiceImpl getSelf() {
        return SpringUtil.getBean(getClass());
    }

    @Override
    public String createRoleFolderUser(RoleFolderUserCreateReqVO createReqVO) {
        // 插入
        RoleFolderUserDO roleFolderUser = RoleFolderUserConvert.INSTANCE.convert(createReqVO);
        roleFolderUserMapper.insert(roleFolderUser);
        // 返回
        return roleFolderUser.getId();
    }

    @Override
    public void batchCreateOrg(List<RoleFolderUserCreateReqVO> createReqVO) {
        List<RoleFolderUserDO> list = createReqVO.stream().map(row -> RoleFolderUserConvert.INSTANCE.convert(row)).collect(Collectors.toList());
        roleFolderUserMapper.insertBatch(list);
    }

    @Override
    public void updateRoleFolderUser(RoleFolderUserUpdateReqVO updateReqVO) {
        // 校验存在
        validateRoleFolderUserExists(updateReqVO.getId());
        // 更新
        RoleFolderUserDO updateObj = RoleFolderUserConvert.INSTANCE.convert(updateReqVO);
        roleFolderUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteRoleFolderUser(String id) {
        // 校验存在
        validateRoleFolderUserExists(id);
        // 删除
        roleFolderUserMapper.deleteById(id);
    }

    private void validateRoleFolderUserExists(String id) {
        if (roleFolderUserMapper.selectById(id) == null) {
            throw exception(ROLE_FOLDER_USER_NOT_EXISTS);
        }
    }

    @Override
    public RoleFolderUserDO getRoleFolderUser(String id) {
        return roleFolderUserMapper.selectById(id);
    }

    @Override
    public List<RoleFolderUserDO> getRoleFolderUserList(Collection<String> ids) {
        return roleFolderUserMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RoleFolderUserDO> getRoleFolderUserPage(RoleFolderUserPageReqVO pageReqVO) {
        return roleFolderUserMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<RoleFolderUserViewDO> getRoleFolderUserPageV2(RoleFolderUserPageReqVO pageVO) {
        IPage<T> mpPage = MyBatisUtils.buildPage(pageVO);
        roleFolderUserMapper.selectPageCustomize(mpPage, pageVO);
        // 转换返回
        return new PageResult(mpPage.getRecords(), mpPage.getTotal());
    }

    @Override
    public List<RoleFolderUserDO> getRoleFolderUserList(RoleFolderUserExportReqVO exportReqVO) {
        return roleFolderUserMapper.selectList(exportReqVO);
    }

    @Override
    public List<RoleFolderUserDO> getRoleFolderListByPsmId(String psmId) {
        RoleFolderUserExportReqVO exportReqVO = new RoleFolderUserExportReqVO();
        exportReqVO.setOrgInId(psmId);
        return roleFolderUserMapper.selectList(exportReqVO);
    }

    @Override
    public List<String> getCurrentUserRoleFolderList(String authType) {
        // 如果是超管或者租户管理员，则赋予所有权限。利用system模块角色验证。
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if (permissionApi.hasAnyRoles(loginUser.getId(), RoleCodeEnum.SUPER_ADMIN.getCode(), RoleCodeEnum.TENANT_ADMIN.getCode()).getCheckedData()) {
            CommonTreeBizExportReqVO reqVo = new CommonTreeBizExportReqVO();
            reqVo.setKindId(ROLE_FOLDER_KIND_ID);
            List<CommonTreeBizDO> list = commonTreeBizService.getCommonTreeAllList(reqVo);
            return list.stream().map(CommonTreeBizDO::getId).collect(Collectors.toList());
        }
        List<OrgDO> orgDOList = orgService.getPsmListBySystemUserId(loginUser.getId());
        List<String> psmIds = orgDOList.stream().map(OrgDO::getPersonMemberId).collect(Collectors.toList());
        List<RoleFolderUserDO> lst = roleFolderUserMapper.selectListByPsmIds(psmIds,authType);
        return lst.stream().map(RoleFolderUserDO::getRoleFolderId).collect(Collectors.toList());
    }
}
